﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace Urd.Common
{
    public static class mysql
    {
        static MySqlConnection myconn;
        static DateTime timestart;
        static DateTime timefinish;
        static TimeSpan timespan;
        static mysql()
        {

        }
        /// <summary>
        /// string connectionstring = String.Format("server={0};user id={1}; password={2}; database={3}; pooling=false",
        /// config.database_ip_adress_or_domain, config.database_username, config.database_password, config.database_name);
        /// </summary>
        /// <param name="?"></param>
        public static void connect(string connectionstring)
        {


            try
            {
                
                myconn = new MySqlConnection(connectionstring);
            }
            catch (MySqlException e) { debug.Log("[mysql][connect]:" + e.Source + " " + e.Message, debug.loglevel.Errors); }
            catch (Exception e) { debug.Log("[system]:" + e.Source + " " + e.Message, debug.loglevel.Errors); }

        }

        public static object[] Sql(string request, bool read)
        {
            timestart = DateTime.Now;
            lock (request)
            {
                object[] ret;
                try
                {
                    if (read)
                    {
                        myconn.Open();
                        MySqlCommand mycom = new MySqlCommand(request, myconn);
                        MySqlDataReader dr = mycom.ExecuteReader();
                        int count = dr.FieldCount;
                        ret = new object[count];
                        while (dr.Read())
                        {
                            dr.GetValues(ret);
                        }
                        dr.Close();
                        myconn.Close();
                        timefinish = DateTime.Now;
                        timespan = timefinish - timestart;
                        debug.Log("[mysql][done](" + timespan.ToString() + "):" + request, debug.loglevel.Status);
                        return ret;
                    }
                    else
                    {
                        ret = new object[1];

                        myconn.Open();
                        MySqlCommand mycom = new MySqlCommand(request, myconn);
                        #region shit
                        object result = mycom.ExecuteNonQuery();
                        if (result == null)
                        {
                            myconn.Close();
                            debug.Log("[mysql][error]:" + request, debug.loglevel.Status);


                        }
                        else if (result.GetType() == typeof(int))
                        {

                            ret[0] = result;

                        }
                        #endregion
                        myconn.Close();
                        timefinish = DateTime.Now;
                        timespan = timefinish - timestart;
                        debug.Log("[mysql][done](" + timespan.ToString() + "):" + request, debug.loglevel.Status);
                        return ret;
                    }

                }
                catch (MySqlException e) { debug.Log("[mysql]:" + e.Source + " " + e.Message, debug.loglevel.Errors); }
                catch (Exception e) { debug.Log("[system]:" + e.Source + " " + e.Message, debug.loglevel.Errors); }
                finally
                {
                    if (myconn != null) myconn.Close();
                }

                ret = new object[2];

                return ret;

            }
        }


    }
}
